<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/28
 * Time: 10:56
 */

class Setting extends ADM_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->view('admin/order/setting.html');
    }

    public function getDays()
    {
        $send['state'] = true;
        $send['data'] = 0;

        $db = clone($this->db);
        $q = $db->get_where('setting', ['use' => 'system', 'key' => 'autoDays']);
        $item = $q->row_array();
        if (empty($item)) {
            $db->insert('setting', ['use' => 'system', 'key' => 'autoDays', 'value' => 15]);
            $send['data'] = 15;
        } else {
            $send['data'] = intval($item['value']);
        }
        $this->output->send($send);
    }

    public function setDays()
    {
        $send['state'] = false;
        do {

            $days = $this->input->post('days');
            if (empty($days)) {
                $send['error'] = '参数错误';
                break;
            }

            if ($days <= 0) {
                $send['error'] = '自动完成天数请大于0天';
                break;
            }

            $this->db->where(['use' => 'system', 'key' => 'autoDays'])->update('setting', ['value' => $days]);
            if ($this->db->affected_rows()) {
                $send['state'] = true;
            } else {
                $send['error'] = '设置失败';
            }

        } while(false);
        $this->output->send($send);
    }
}
